<?php

$x = $_POST['x'];
$y = $_POST['y'];
$dirname = $_POST['map'];
$backcolorarray = array(); //array to hold all background colors
$backnamearray = array(); //array to hold all names of provinces

$baseimg = imagecreatefrompng("../images/$dirname/mapback.png");
$rgb = imagecolorat($baseimg,$x,$y);

$rgbcolors = imagecolorsforindex($baseimg, $rgb);

$dir = opendir("../images/$dirname/maps/coords/");

while(false != ($file = readdir($dir))){
	if($file != "." && $file != ".."){ //fetch all files excluding parentfolders
		//$files[$filecount] = $file; //place filename in array
		$coordfile = file_get_contents("../images/$dirname/maps/coords/" . $file, 'r'); //read the json-file

		$imgcoords = json_decode($coordfile); //fetch the coords to draw polygon
		$backcolorarray[] = $imgcoords -> backcolor;
		$file = strstr($file, '.', TRUE);
		$backnamearray[] = $file;
	}
}

for($i=0;$i<count($backcolorarray);$i++){
	if($rgbcolors["red"] == $backcolorarray[$i]){
		echo $backnamearray[$i];
		//return false;
	}
}
?>